﻿<?php
include_once("phpQuery-onefile.php");

/*
 * Convertir,.odt en .zip
 * Extraire content.xml
 * Enlever le namespacing de tous les tags
 */

class Sequencer
{
	public static $seq = array();

	public static function getNext($name)
	{
		if (! isset(Sequencer::$seq[$name]))
			Sequencer::$seq[$name] = 0;
		Sequencer::$seq[$name] = Sequencer::$seq[$name]+1;

		return Sequencer::$seq[$name];
	}

}

class Storage
{
	public static $objects = array();

	public static function createStep()
	{
		$step = new Step();
		$step->id = Sequencer::getNext("Step");
		return $step;
	}

	public static function saveStep($step)
	{
		if (!isset(Storage::$objects["Step"]))
			Storage::$objects["Step"] = array();
		Storage::$objects["Step"][$step->id] = $step;
	}

	public static function createItem($parent)
	{
		$item = new Item();
		$item->id = Sequencer::getNext("Item");
		$item->setParent($parent);
		return $item;
	}

	public static function saveItem($item)
	{
		if (!isset(Storage::$objects["Item"]))
			Storage::$objects["Item"] = array();
		Storage::$objects["Item"][$item->id] = $item;
	}

	public static function buildInsert()
	{
		foreach (Storage::$objects as $arr)
		{
			foreach ($arr as $object)
			{
				$object->buildInsert();
			}
		}
	}
}

class Step
{
	public $id;
	public $label;
	public $nbWeeks;

	public function buildInsert()
	{
		$label = str_replace("&#39;", "''", html_entity_decode(str_replace("&amp;", "&", $this->label)));
		echo "INSERT INTO step(id,label) VALUES ({$this->id}, '$label');\n";
	}
}

class Item
{
	public $id;
	public $label;
	public $order;
	public $stepId;
	public $parentItemId;
	public $nbWeeks;
	public $nbWeeksBefore;

	public function setParent($p)
	{
		if ($p instanceof Step)
		{
			$this->stepId = $p->id;
		}else{
			$this->parentItemId = $p->id;
		}
	}

	public function buildInsert()
	{
		$label = str_replace("&#39;", "''", html_entity_decode(str_replace("&amp;", "&", $this->label)));
		if (isset($this->parentItemId) && $this->parentItemId != null)
			echo "INSERT INTO topic(id, label, parent_id, orderIndex, weeks_number, weeks_before) VALUES ({$this->id}, '$label', {$this->parentItemId}, {$this->order}, {$this->nbWeeks}, {$this->nbWeeksBefore});\n";
		else
			echo "INSERT INTO topic(id, label, step_id,   orderIndex, weeks_number, weeks_before) VALUES ({$this->id}, '$label', {$this->stepId}, {$this->order}, {$this->nbWeeks}, {$this->nbWeeksBefore});\n";
	}
}

header("Content-Type: text/plain; charset=UTF-8");

$doc = phpQuery::newDocumentFileXML("content.xml") ;

$id = 0;
$steps = array();
$items = array();
$parent = null;
foreach (pq("document-content > body > text > list > list-item") as $step)
{
	$n = 0;
	$s = new Step();
	foreach (pq($step)->children("p") as $text)
	{
		$t = trim(ereg_replace("[\n\r\t ][\n\r\t ]*", " ", pq($text)->text()));;
		$s->id = Sequencer::getNext("Step");
		$s->label = ereg_replace("'", "''", ereg_replace ("([^[]*)\[[0-9][0-9]*[\n\r\t ][\n\r\t ]*[0-9][0-9]*\][\n\r\t ]*$", "\\1", $t));
		$data = ereg_replace ("[^[]*\[([0-9][0-9]*)[\n\r\t ][\n\r\t ]*([0-9][0-9]*)\][\n\r\t ]*$", "\\1 \\2", $t);
		$data = split(" ", $data);
		$s->nbWeeksBefore = 0 + $data[0];
		$s->nbWeeks = 0 + $data[1];
	}
	
	Storage::saveStep($s);
	$children = parseItem($s, $step);
	setNbWeeks($s, $children);
	$steps[] = $s;
}

function parseItem($parent, $step) {
	$items = array();
	foreach (pq($step)->children("list")->children("list-item")->children("p") as $text)
	{
		$n++;
		$s = new Item();
		$t = trim(ereg_replace("[\n\r\t ][\n\r\t ]*", " ", pq($text)->text()));;
		$s->id = Sequencer::getNext("Item");
		$s->label = ereg_replace("'", "''", ereg_replace ("([^[]*)\[[0-9][0-9]*[\n\r\t ][\n\r\t ]*[0-9][0-9]*\][\n\r\t ]*$", "\\1", $t));
		$s->order = $n;
		$s->setParent($parent);
		$data = ereg_replace ("[^[]*\[([0-9][0-9]*)[\n\r\t ][\n\r\t ]*([0-9][0-9]*)\][\n\r\t ]*$", "\\1 \\2", $t);
		$data = split(" ", $data);
		$s->nbWeeksBefore = 0 + $data[0];
		if ($s->nbWeeksBefore == 0)
			$s->nbWeeksBefore = $parent->nbWeeksBefore;
		$s->nbWeeks = 0 + $data[1];
		if ($s->nbWeeks == 0)
			$s->nbWeeks = $parent->nbWeeks;
		
		Storage::saveItem($s);
		$children = parseItem($s, pq($text)->parent());
		setNbWeeks($s, $children);
		$items[] = $s;
	}
	
	return $items;
}

function setNbWeeks($item, $children) {
	if ($children == null || count($children) == 0) {
		return;
	}
	
	$nbWeeks = 0;
	$nbWeeksBefore = 0;
	
	foreach($children as $child) {
		if ($nbWeeks < $child->nbWeeks)
			$nbWeeks = $child->nbWeeks;
		if ($nbWeeksBefore < $child->nbWeeksBefore)
			$nbWeeksBefore = $child->nbWeeksBefore;
	}

	$item->nbWeeks = $nbWeeks;
	$item->nbWeeksBefore = $nbWeeksBefore;
}

Storage::buildInsert();





